/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import controller.Constants;
import controller.StoreFactory;
import core.Node;
import core.Tracker;
import load.parent.Store;
import load.storage.StoreIO;
import load.util.Comment;
import load.util.Watcher;
import model.graphic.objects.GNode;
import model.graphic.objects.Graphical;
import model.graphic.objects.RectangleGraphical;

/**
 *
 * @author Jelle
 */
public class EditNodeCommand extends BasicCommand {

    private RectangleGraphical n;
    private String arg,  value;

    public EditNodeCommand(String arg, RectangleGraphical n, String value) {
        this.n = n;
        this.value = value;
        this.arg = arg;
    }

    @Override
    public void execute() {
        Tracker.setInstance(new Tracker());
        gmodel.editNode(n, value, arg);
        Node no = sync.get(n);
        if (arg.equals("value") && ! Comment.class.isAssignableFrom(no.getClass())){
            Store st = (Store) no;
            Class c = st.getValue().getClass();
            if (Integer.class.isAssignableFrom(c)) {
                try {
                    st.setValue(Integer.parseInt(value));
                } catch (Exception ex) {
                    st.setValue(StoreFactory.DEFAULT_INT);
                }
            } else if (Boolean.class.isAssignableFrom(c)) {
                try {
                    st.setValue(Boolean.parseBoolean(value));
                } catch (Exception ex) {
                    st.setValue(StoreFactory.DEFAULT_BOOLEAN);
                }
            } else if (String.class.isAssignableFrom(c)) {
                try {
                    st.setValue(value);
                } catch (Exception ex) {
                    st.setValue(StoreFactory.DEFAULT_STRING);
                }
            } else if (Double.class.isAssignableFrom(c)) {
                try {
                    st.setValue(Double.parseDouble(value));
                } catch (Exception ex) {
                    st.setValue(StoreFactory.DEFAULT_DOUBLE);
                }
            } else if (Object.class.isAssignableFrom(c)) {
                try {
                    st.setValue(value);
                } catch (Exception ex) {
                    st.setValue(null);
                }
            }

            for (Graphical gr : gmodel.getObjects(Constants.getNodeString())) {
                GNode wg = (GNode) gr;
                if (wg.getType().equals("Watcher")) {
                    Node wn = (Node) sync.get(wg);
                    Watcher w = (Watcher) wn;
                    wg.setValue(w.getValue());
                    gmodel.setModelChanged();
                    gmodel.notifyObservers();
                } else if (wg.getType().equals("StoreIO")) {
                    StoreIO wn = (StoreIO) sync.get(wg);
                    wg.setValue(wn.getValue().toString());
                    gmodel.setModelChanged();
                    gmodel.notifyObservers();
                }
            }
        }
    }
}
